package com.roc.web.action.bankcard;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.roc.bean.WechatValidateBean;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.IBankCardFourKeyService;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
/**
 * @author jjj
 * @detail  银行卡四要素管理
 * @version 0.1 银行卡四要素查询  2017-05-22
 * @version 0.2 银行卡四要素删除  2017-05-22
 * */

@SuppressWarnings("serial")
@Component
@Controller
@Scope("prototype")
@Namespace("/bankcardfourkey")
public class BankCardFourKeyMainAction extends PaginationBaseAction{

    //日志
    private Logger logger=LogManager.getLogger(BankCardFourKeyMainAction.class);
	//页面录入项，查询sql
	private BankCardFourKeySearchCondition searchCondition;
	
	@Autowired
	private IPagenitionService pagenitionService;//分页公用类
	
	@Autowired
	IBankCardFourKeyService bankCardFourKeyService;//银行卡四要素Service层
	
	private String accountNos;//页面传入参数--选中的银行卡号
	
	public String getAccountNos() {
		return accountNos;
	}
	public void setAccountNos(String accountNos) {
		this.accountNos = accountNos;
	}
	
	/**
	 * 银行卡四要素页面初始化
	 */
	@Action(value = "init", results = { @Result(name = SUCCESS, location = "/pages/bankCard/bankCardFourKeyMain.jsp")})
	public String bankCardFourKryinit(){
		Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.AGENT.getCode())&&!"sdsyb".equals(op.getAccountNo())) {
			addActionError("当前登录账号无权限访问!!!");
			return ERROR;
		}
		
		return SUCCESS;
		}
	
	/**
	 * 银行卡四要素查询
	 */
	@Action(value = "queryBankCardFourKeyList", 
			results = { @Result(name = SUCCESS, location = "/pages/bankCard/bankCardFourKeyMain.jsp"),
			@Result(name = ERROR, location = "/error.jsp")
    })
	public String queryBankCardFourKey(){
		Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.AGENT.getCode())&&!"sdsyb".equals(op.getAccountNo())) {
			addActionError("当前登录账号无权限访问!!!");
			return ERROR;
		}
		if(searchCondition==null){
			searchCondition=(BankCardFourKeySearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new BankCardFourKeySearchCondition();
			}
		}
		try {
            //得到条数
			int rows=pagenitionService.countSQL(searchCondition);
			if (rows > 0) {
				//遍历查询结果并放入list
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				dealWithTheListSpecially(searchRsList);
				List<WechatValidateBean> list = new ArrayList<WechatValidateBean>();
				if (null != searchRsList && searchRsList.size() > 0) {
					Iterator<?> iterator = searchRsList.iterator();
					while (iterator.hasNext()) {
						Object[] obj = (Object[]) iterator.next();
						WechatValidateBean wv = new WechatValidateBean();
						wv.setPhoneNo(Constance.Null2EmptyTrim(obj[0]));
						wv.setAccountNo(Constance.Null2EmptyTrim(obj[1]));
						wv.setAccountName(Constance.Null2EmptyTrim(obj[2]));
						wv.setIdentityCard(Constance.Null2EmptyTrim(obj[3]));
						wv.setCreateTime(Constance.Null2EmptyTrim(obj[4]));
						list.add(wv);
					}
					searchRsList.clear();
					searchRsList.addAll(list);
				}
			}
			if (searchRsList.size() == 0) {
				getRequest().setAttribute("msg1", "查询银行卡四要素信息为空");
			}
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		}catch(ServiceException e){
			logger.error(e.getMessage(), e);
			addActionError(e.getMessage());
			getRequest().setAttribute("msg1", "查询银行卡四要素信息异常，请联系管理员");
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 银行卡四要素删除
	 */
	@Action(value = "delFourKeyInfo", results = {
			@Result(name = SUCCESS,type="chain",location="queryBankCardFourKeyList"),//删除成功后查询
			@Result(name = "error", location = "/error.jsp") 
			})
	public String delFourKeyInfo() throws Exception {
		Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		String msginfo="";
		try {
			if(null == accountNos || accountNos.equals("")){
				addActionError("银行卡号为空,不能进行四要素删除操作");
				msginfo= "银行卡号为空,不能进行四要素删除操作！";
				return ERROR;
			}
			String [] arr = accountNos.split(",");//选中要删除的银行卡号
			bankCardFourKeyService.delFourKeyInfo(arr, op);//删除四要素 
			msginfo= "银行卡四要素信息删除成功！";
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			msginfo=  "银行卡四要素信息删除失败！";
			addActionError(e.getMessage());
			return ERROR;
		}
		getRequest().setAttribute("msg",msginfo);
		return SUCCESS;
	}

	
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_BANK_CARD_QUERY, searchCondition);
		
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_BANK_CARD_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}
	public BankCardFourKeySearchCondition getSearchCondition() {
		return searchCondition;
	}
	public void setSearchCondition(BankCardFourKeySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	
}
